﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using Infragistics.Win.UltraWinGrid;

namespace DMSClient.Unilever
{
    public partial class productGold : Form
    {
        WSHttpBinding httpBinding = new WSHttpBinding();
        EndpointAddress address = new EndpointAddress(Properties.Settings.Default.Server);

        
        DataTable dt;
        string ambiente;
        string ambienteDes;

        public productGold()
        {
            InitializeComponent();
            ambiente = Properties.Settings.Default.CurrentEnv;
            ambienteDes = Properties.Settings.Default.CurrentEnvDes;
            httpBinding.MaxReceivedMessageSize = 2147483647;
            httpBinding.MaxBufferPoolSize = 2147483647;
            httpBinding.CloseTimeout = TimeSpan.MaxValue;
            httpBinding.ReceiveTimeout = TimeSpan.MaxValue;
            httpBinding.Security.Mode = SecurityMode.None;
            httpBinding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
            this.Text = ambienteDes + " - Product Gold";
        }

        private void ubSearch_Click(object sender, EventArgs e)
        {
            bgwProductG.RunWorkerAsync();
            ugProductG.DataSource = null;
            cambio(false);

        }

        private void cambio(bool estado) {
            ubSearch.Enabled = estado;
            ubSearch.Enabled = estado;
            loader1.Visible = !estado;
        }

        private void bgwProductG_DoWork(object sender, DoWorkEventArgs e)
        {
            string listarProductG = null;
            BackgroundWorker worker = sender as BackgroundWorker;
            DMSServices.DMSServicesClient cliente = new DMSServices.DMSServicesClient(httpBinding, address);
            try {
                cliente.Open();
                listarProductG = cliente.getProductGold(ambiente);
                cliente.Close();
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(Properties.Settings.Default.ReportErrorMsg, ex.InnerException);
            }
            if (listarProductG == null) throw new Exception(Properties.Settings.Default.ReportNullErrorMsg);

            dt = new DataTable("productGold");
            dt.Columns.Add("cdSKU");
            dt.Columns.Add("dsproduct");
            dt.Columns.Add("idstatus");
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            System.IO.StringReader xmlSR = new System.IO.StringReader(listarProductG);
            ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);
            xmlSR.Close();
        }

        private void bgwProductG_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                if (!this.IsDisposed)
                {
                    this.BringToFront();
                    MessageBox.Show(this, e.Error.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cambio(true);
                }
            }
            else if (!e.Cancelled && !this.IsDisposed)
            {
                ugProductG.DataSource = dt;
                cambio(true);
            }
        }

        private void ubExport_Click(object sender, EventArgs e)
        {
            if (ugProductG.Rows.Count < 1)
            {
                MessageBox.Show(this, "There is no data to export", "Product Gold Report", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            System.Windows.Forms.SaveFileDialog dialogo = new System.Windows.Forms.SaveFileDialog();
            dialogo.DefaultExt = "xls";
            Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter Exporter = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
            if (dialogo.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Exporter.Export(ugProductG, dialogo.FileName);
                if (MessageBox.Show(this, "Do you want to open the file?", "Product Gold Report", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(dialogo.FileName);
                }
            }
        }

        private void centrarLoader() {
            loader1.Left = (this.Width - loader1.Width) / 2;
            loader1.Top = ((this.Height - loader1.Height) / 2) - loader1.Height;
        }

        private void productGold_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(bgwProductG.IsBusy)bgwProductG.CancelAsync();
        }

        private void productGold_Resize(object sender, EventArgs e)
        {
            centrarLoader();
        }

        private void ugProductG_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            foreach (UltraGridColumn ugc in e.Layout.Bands[0].Columns)
            {
                ugc.PerformAutoResize(PerformAutoSizeType.AllRowsInBand, true);
            }
        }

        private void productGold_Load(object sender, EventArgs e)
        {

        }
    }
}
